<template>
  <v-card>
    <!-- <v-card-title :class="warningType">{{cardText}} × {{rest}}<span class="check" v-if="checkStatus">√</span></v-card-title> -->
    <v-card-title v-cloak :class="warningType">
      <span>{{cardText|strAbbr4}} </span>
      <span v-cloak>×</span>
      <span>{{quantity}}</span>
    </v-card-title>
    <!-- <v-card-text>{{expireTime|datetimeFormat}}</v-card-text> -->
    <v-card-text v-if="warningType==='safe'">{{expireToady}}</v-card-text>
    <v-card-text v-if="warningType==='warn'">{{toExpireTime}}</v-card-text>
    <v-card-text v-if="warningType==='danger'">
      {{expireTime|datetimeFormat}}
    </v-card-text>
  </v-card>
</template>

<script>
    export default {
        name: 'ForewarningCard',
        data() {
            return {
                now: new Date().getTime()
            }
        },
        mounted() {
            let _this = this;
            this.timer = setInterval(() => {
                _this.now = new Date().getTime(); // 修改数据date
            }, 1000)
        },
        computed: {
            //今日到期
            expireToady() {
                let obj = this.formatSeconds((this.expireTime - this.now) / 1000);
                if (obj.hour && obj.min && obj.sec) {
                    return obj.hour + "小时" + obj.min + "分钟" + obj.sec;
                }
                if (obj.hour === "") {
                    return obj.min + "分钟" + obj.sec;
                }
                if (obj.sec === 0) {
                    return obj.hour + "小时" + obj.min + "分钟" + obj.sec;
                }
                if (obj.min === 0) {
                    return obj.hour + "小时" + obj.min + "分钟" + obj.sec;
                }
                if (obj.hour === "" && obj.min === "") {
                    return obj.sec;
                }
                if (obj.sec === "" && obj.min === "" && obj.hour === "") {
                    return "已到期";
                }
            },

            //即将过期
            toExpireTime() {
                let obj = this.formatSeconds((this.expireTime - this.now) / 1000);
                if (obj.hour && obj.min && obj.sec) {
                    return obj.hour + "小时" + obj.min + "分钟" + obj.sec;
                }
                if (obj.hour === "") {
                    return obj.min + "分钟" + obj.sec;
                }
                if (obj.sec === 0) {
                    return obj.hour + "小时" + obj.min + "分钟" + obj.sec;
                }
                if (obj.min === 0) {
                    return obj.hour + "小时" + obj.min + "分钟" + obj.sec;
                }
                if (obj.hour === "" && obj.min === "") {
                    return obj.sec;
                }
                if (obj.sec === "" && obj.min === "" && obj.hour === "") {
                    return "已到期";
                }
            },

        },
        watch: {
            now(val, old) {
            },

            checkStatus(val, old) {
            }
        },
        props: {
            cardText: {
                type: String,
                default: ""
            },
            warningType: {
                type: String,
                default: ""
            },
            quantity: {
                type: Number,

            },
            expireTime: {
                type: Number,
                default: 0
            },
            //选中状态
            checkStatus: {
                type: Boolean,
                default: false
            },
        },

        methods: {

            //将秒数转换为时间
            formatSeconds(value) {
                let allSec = parseInt(value); // 秒
                let toMin = 0; // 分
                let toHour = 0; // 小时
                if (allSec > 60) {
                    toMin = parseInt(allSec / 60);
                    allSec = parseInt(allSec % 60);
                    if (toMin > 60) {
                        toHour = parseInt(toMin / 60);
                        toMin = parseInt(toMin % 60);
                    }
                }
                let hour = toHour < 1 ? "0" : toHour;
                let min = toMin < 1 ? "0" : toMin;
                return {
                    hour: hour,
                    min: min,
                    sec: allSec
                }

            }

        }

    }

</script>

<style scoped>
  .v-card {
    border-radius: 4px;
    overflow: hidden;
    box-shadow: 0 0 2px #ddd !important;
  }

  .v-card__title {
    /* font-size: 20px; */
    padding: 8px 16px;
    /* text-align: center !important; */
    font-size: 18px;
    font-weight: bold;
    color: #2cbc87;
  }


  .check {
    display: block;
    width: 20px;
    height: 20px;
    font-size: 14px;
    position: absolute;
    right: 0;
    top: 0;
    text-align: center;
    line-height: 20px;
  }

  .v-card__text {
    text-align: left;
    padding: 0;
    padding-left: 16px;
    /* text-indent: 16px; */
    font-size: 12px;

  }

  .safe {
    color: #0ea86f;
    border-bottom: 0;
  }

  .safe::after {
    content: "";
    width: 90px;
    height: 90px;
    border-radius: 100%;
    background: #0ea86f;
    position: absolute;
    top: -9px;
    left: -84px;
  }

  .safe + div {
    color: #b2b2b2 !important;
    border-top: 0;
  }


  .warn {
    color: #f4ae49;
    border-bottom: 0;
  }

  .warn::after {
    content: "";
    width: 90px;
    height: 90px;
    border-radius: 100%;
    background: #f4ae49;
    position: absolute;
    top: -9px;
    left: -84px;
  }


  .warn + div {
    color: #b2b2b2 !important;
    border-top: 0;
  }

  .danger {
    color: #f36260;
    border-bottom: 0;
  }

  .danger::after {
    content: "";
    width: 90px;
    height: 90px;
    border-radius: 100%;
    background: #f36260;
    position: absolute;
    top: -9px;
    left: -84px;
  }


  .danger + div {
    color: #b2b2b2 !important;
    border-top: 0;
  }

  [v-cloak] {
    display: none;
  }

</style>
